package com.sunxy.bluetoothdemo

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.common.frame.utils.FormatUtils
import com.sunxy.bluetoothdemo.bean.BlueDeviceBean

/**
 *
 * create by sunxy on 2022/10/18
 **/
class ItemAdapter: RecyclerView.Adapter<ItemAdapter.ViewHolder>() {

    var click: ((BlueDeviceBean)->Unit)? = null
    private var list: MutableList<BlueDeviceBean>? = null

    fun setList(list: MutableList<BlueDeviceBean>?) {
        this.list = list
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(parent.context)
            .inflate(R.layout.item_layout, parent, false))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(list!![position])
    }

    override fun getItemCount(): Int {
       return list?.size ?: 0
    }

    inner class ViewHolder(view: View): RecyclerView.ViewHolder(view) {

        private var bean: BlueDeviceBean? = null

        private val nameView by lazy { itemView.findViewById<TextView>(R.id.name_view) }
        private val timeView by lazy { itemView.findViewById<TextView>(R.id.temp_view) }
        private val statusView by lazy { itemView.findViewById<TextView>(R.id.status_view) }

        init {
            itemView.setOnClickListener {
                bean?.let {
                    click?.invoke(it)
                }
            }
        }

        fun bind(data: BlueDeviceBean) {
            this.bean = data
            nameView.text = "${data.bluetoothAddress} (${data.beaconName})"
            timeView.text = FormatUtils.formatTime(data.lastScanTime, FormatUtils.yyyy_MM_dd_HH_mm_ss)
            statusView.visibility = View.GONE
//            statusView.text = when(data.status) {
//                0 -> "未上传"
//                1 -> "上传中"
//                2 -> "上传成功"
//                else -> ""
//            }
        }

    }
}